package cn.doourbest.learnspring.filter;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.annotation.PostConstruct;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 * @description:
 * @author: zhangsc
 * @create: 2019/11/18
 */
@Component
public class MyFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {


//        if(request.getRequestURI().equals("/")){
//            response.sendRedirect("https://localhost:8083");
//        }
        filterChain.doFilter(request,response);
    }

    @Autowired
    private BeanFactory beanFactory;

    @PostConstruct
    public void init(){
//        System.out.println(1);
//        String[] beanDefinitionNames = ((DefaultListableBeanFactory) beanFactory).getBeanDefinitionNames();
//        BeanDefinition beanDefinition = ((DefaultListableBeanFactory) beanFactory).getBeanDefinition(beanDefinitionNames[0]);
//        Arrays.stream(beanDefinitionNames).forEach(
//                x -> System.out.println(((DefaultListableBeanFactory) beanFactory).getBeanDefinition(x).getClass().getName())
//        );
    }
}
